package com.springblog.controller;


import com.springblog.entity.UserEntity;
import com.springblog.service.UserService;
import com.springblog.common.utils.JwtUtils;
import com.springblog.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ifredom@gmail.com
 * @since 2022-04-23
 */
@RestController
@RequestMapping("/user")
@SuppressWarnings("all")
public class UserController {
    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public R getLogin(@RequestBody UserEntity userEntity) {

        if ("admin".equals(userEntity.getUsername()) && "123456".equals(userEntity.getPassword())) {
            //使用工具生成token
            String token = jwtUtils.generateToken(1L);
            return R.ok().put("token", token);
        }
        return R.error("用户名或密码错误");
    }

    @GetMapping("/home")
    public R getIndex() {
        UserEntity userEntity = userService.getById(1L);

        System.out.println("获取======" + userEntity);

        return R.ok().put("user", userEntity);
    }
    @PostMapping("/save")
    public R save(@Validated @RequestBody UserEntity userEntity) {
        userService.save(userEntity);
        System.out.println("获取======" + userEntity);
        return R.ok();
    }
}